#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <ctype.h>

#define U1_MASK	0x0
#define U2_MASK	0xC0
#define UN_MASK	0x80
#define U3_MASK	0xE0
#define U4_MASK	0xF0

int
valid_utf8(const unsigned short byte, int n)
{
	if (n == 1) {
		if ((byte >> 3) == (U4_MASK >> 3)) return 4;
		if ((byte >> 4) == (U3_MASK >> 4)) return 3;
		if ((byte >> 5) == (U2_MASK >> 5)) return 2;
		if ((byte >> 7) == 0) return 1;
	} else if (n > 1 && n <= 4 && (byte >> 6) == 020) {
		return -1;
	} else {
		return 0;
	}

	return 0; //unreach
}

int
main(int argc, char **argv)
{
	unsigned short byte;
	unsigned int code = 0;
	int byte_n = 1, len = 1;

	while (byte_n <= len) {
		scanf("%hu", &byte);
		if (byte_n == 1) {
			if ((byte >> 3) == (U4_MASK >> 3)) {
				len = 4;
				code |= (byte & 0x7) << (6 * 3);
			} else if ((byte >> 4) == (U3_MASK >> 4)) {
				len = 3;
				code |= (byte & 0xF) << (6 * 2);
			} else if ((byte >> 5) == (U2_MASK >> 5)) {
				len = 2;
				code |= (byte & 0x1F) << 6;
			} else if ((byte >> 7) == 0) {
				len = 1;
				code = byte;
			} else {
				fputs("-1\n", stdout);
				return 0;
			}
		} else if (byte_n > 1 && byte_n <= 4 && (byte >> 6) == 02) {
			code |= (byte & 0x3F) << (6 * (len - byte_n));
		} else {
			fputs("-1\n", stdout);
			return 0;
		}
		byte_n++;
	}

	if (((len == 4 && code >= 0x10000 && code <= 0x10FFFF) ||
	     (len == 3 && code >= 0x800 && code <= 0xFFFF) ||
	     (len == 2 && code >= 0x80  && code <= 0x7FF ) ||
	     (len == 1 && code >= 0x0   && code <= 0x7F  )) &&
	    scanf("%hu", &byte) != 1)
		printf("%u\n", code);
	else
		fputs("-1\n", stdout);

	return 0;
}
